import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import storage from '../shared/storage';
import { HomeService } from './home.service';
import SysData from '../shared/sysData';
import {NzModalService} from 'ng-zorro-antd';
import { TranslateService } from "@ngx-translate/core";
import {NzI18nService,en_US,zh_CN} from 'ng-zorro-antd';
import { NzFormatEmitEvent, NzTreeNode,NzMessageService } from 'ng-zorro-antd';
import tools from '../shared/tools';
import Base64, {default as base64} from '../shared/base64';
import Mysocket from '../shared/mySocket';
@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
    isOffLineShow:boolean=false;
  index = 0;
  times=0; //获取证书次数
  storeTree;
  // 是否有数据权限
  isSpinning:boolean = false;
  isMessageShow:boolean=false;
  messageContent="";
  isOfficial=true;
  isConfigShow:boolean=false;
  LoginForm:any=storage.getSessionStorage('LoginForm');
  //获取字典
    getTree(){
        this.homeService.getTree().subscribe(res=>{
            console.log(res);
            if(res.status==1){
                if(res.data && res.data.length>0){
                    for(let i=0;i<res.data.length;i++){
                        let aa10 = res.data[i];
                        if(aa10.children && aa10.children.length){
                            //缓存到本地
                            storage.setSessionStorage("AA10_"+aa10.value,aa10.children);
                        }
                    }
                }
                this.getWebsocketInfo();
            }else {
                this.message.error(res.message);
            }
        });
    }
    constructor(
              private translateService:TranslateService,
              private i18n: NzI18nService,
              private router: Router,
              private homeService:HomeService,
              private modalService: NzModalService,
              private message: NzMessageService,
    ) { }
    ngOnInit() {
        if(!this.LoginForm){
            this.router.navigate(['../login']);
            return;
        }
        window.addEventListener("beforeunload", function(event){
            Mysocket.oncloseWS();
            return event.returnValue;
        }, false);
        this.translateService.setDefaultLang(storage.get("defaultLang"));
        this.translateService.use(storage.get("defaultLang"));
        this.getTree();
    }
    //获取websocket的地址
    getWebsocketInfo(){
        let  data={LoginForm:storage.getSessionStorage('LoginForm')};
        this.homeService.getWebsocket(data).subscribe(res=>{
            console.log(res);
            if(res.status == 1){
                storage.setSessionStorage('websockUrl',res.data.websocket);
                this.isConfigShow=true;
            }else {
                this.message.error(res.message);
            }
        });
    }
    //退出提示
     closeDebug(e){
        this.modalService.confirm({
            nzTitle: this.translateService.instant('tip'),
            nzContent: this.translateService.instant("sure")+this.translateService.instant("sign_out")+this.translateService.instant("q_mark"),
            nzOkText:this.translateService.instant("sign_out"),
            nzOkType: 'danger',
            nzOnOk: () => {
                Mysocket.oncloseWS();
                //调用退出系统
                this.logOut();
            },
            nzCancelText:this.translateService.instant("cancel"),
            nzOnCancel: () => {
            }
        });
    }
    //退出
    logOut(){
        this.homeService.logOut().subscribe(res =>{
            if(res.status){
                storage.clearSessionStorage();
                this.router.navigate(['../login']);
            }
        });
    }
    //确认
    OK(){
        this.isOffLineShow=false;
        this.logOut();
    }
    //显示提示
    showTip(){
        this.isOffLineShow=true;
    }
    ngOnDestroy(): void {
        Mysocket.oncloseWS();
    }
}
